home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / MML.PY < prev    next >
Encoding:
Python Source  |  1999-03-09  |  6.6 KB  |  171 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. '''
  65. $Id: MML.py,v 1.3 1999/03/10 00:15:46 klm Exp $'''
  66.  
  67. from StructuredText import * # :-)
  68.         
  69. def ctag(s):
  70.     # Blech, wish we could use character tags
  71.     if s is None: s=''
  72.     s=gsub(strong,'\\1<bold>\\2<plain>\\3',s)
  73.     s=gsub(code,  '\\1<family Courier>\\2<family Times>\\3',s)
  74.     s=gsub(em,    '\\1<italic>\\2<plain>\\3',s)
  75.     return join(map(strip,split(s,'\n')),'\n')
  76.  
  77. class MML(StructuredText):
  78.  
  79.     '''\
  80.     An MML structured text formatter.
  81.     '''\
  82.  
  83.     def __str__(self,
  84.                 ):
  85.         '''\
  86.         Return an HTML string representation of the structured text data.
  87.  
  88.         '''
  89.         s=self._str(self.structure,self.level)
  90.         return s
  91.  
  92.     def ul(self, before, p, after):
  93.         return ("%s\n\n<Bulleted>\n%s%s"
  94.                 % (before, ctag(p), after))
  95.  
  96.     def ol(self, before, p, after):
  97.         return ("%s\n\n<Numbered>\n%s%s"
  98.                 % (before, ctag(p), after))
  99.  
  100.     def dl(self, before, t, d, after):
  101.         return ("%s\n\n<Term>\n%s\n\n<Definition>\n%s%s" 
  102.                 % (before,ctag(t),ctag(d),after))
  103.  
  104.     def head(self, before, t, level, d):
  105.         return ("%s\n\n<Heading%d>\n%s%s"
  106.                 % (before,level,ctag(t),d))
  107.  
  108.     def normal(self,before,p,after):
  109.         return "%s\n\n<Body>\n%s%s" % (before, ctag(p), after)
  110.  
  111.     def pre(self,structure,r=None):
  112.         if r is None: r=['']
  113.         for s in structure:
  114.             for line in split(s[0],'\n'):
  115.                 r.append('\n<PRE>')
  116.                 r.append(line)
  117.             self.pre(s[1],r)
  118.         return join(r,'\n')
  119.  
  120.     def _str(self,structure,level):
  121.         r=''
  122.         for s in structure:
  123.             # print s[0],'\n', len(s[1]), '\n\n'
  124.             if bullet.match(s[0]) >= 0:
  125.                 p=bullet.group(1)
  126.                 r=self.ul(r,p,self._str(s[1],level))
  127.             elif ol.match(s[0]) >= 0:
  128.                 p=ol.group(3)
  129.                 r=self.ol(r,p,self._str(s[1],level))
  130.             elif olp.match(s[0]) >= 0:
  131.                 p=olp.group(1)
  132.                 r=self.ol(r,p,self._str(s[1],level))
  133.             elif dl.match(s[0]) >= 0:
  134.                 t,d=dl.group(1,2)
  135.                 r=self.dl(r,t,d,self._str(s[1],level))
  136.             elif example.search(s[0]) >= 0 and s[1]:
  137.                 # Introduce an example, using pre tags:
  138.                 r=self.normal(r,s[0],self.pre(s[1]))
  139.             elif s[0][-2:]=='::' and s[1]:
  140.                 # Introduce an example, using pre tags:
  141.                 r=self.normal(r,s[0][:-1],self.pre(s[1]))
  142.             elif nl.search(s[0]) < 0 and s[1] and s[0][-1:] != ':':
  143.                 # Treat as a heading
  144.                 t=s[0]
  145.                 r=self.head(r,t,level,
  146.                             self._str(s[1],level and level+1))
  147.             else:
  148.                 r=self.normal(r,s[0],self._str(s[1],level))
  149.         return r        
  150.